//
//  CFRMRecipeHistoryModel.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit

class CFRMRecipeHistoryViewModel{
    var dataSource = [CFRMRecipeHistoryModel]()
}
extension CFRMRecipeHistoryViewModel{
    func loadData(onSuccess: ((_ models: [CFRMRecipeHistoryModel]) ->Void)?){
        let dataHelper = CFRMRecipeHistoryModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: CFRMRecipeHistoryModel.self) as! [CFRMRecipeHistoryModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 < date2
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
    
}

@objcMembers
class CFRMRecipeHistoryModel: CFRMModel,Codable {
    var weight = ""
    var recipe = 0
    var recipeName = ""
    var date = ""
    var percentage = ""
    var breakfast = false
    var lunch = false
    var dinner = false
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: CFRMRecipeHistoryModel.self)
    }
}
